import 'package:flutter/material.dart';
import 'package:xws/models/notify/sys_notify.dart';
import 'package:xws/ui/mine/friends/friends.dart';
import 'package:xws/utils/app_util.dart';
import 'package:xws/utils/dio_utils/request/user_req.dart';
import 'package:xws/utils/theme_util.dart';
import 'package:xws/widgets/app_btn.dart';
import 'package:xws/widgets/refresh_widget/refresh_load_view.dart';
import 'package:xws/widgets/safe_scaffold.dart';

import 'def_item.dart';
import 'friend_add.dart';
import 'point_changed.dart';

/// 系统通知
class SystemNotify extends StatelessWidget {
  const SystemNotify({Key? key, required this.id}) : super(key: key);

  final String id;

  /// 点击
  Future<void> _onTap(BuildContext context, SysNotify s) async {
    switch (s.messageType) {
      case SysNotifyType.FRIEND_ADD:
        push(context, const FriendsPage());
        break;
      case SysNotifyType.INTEGRAL_CHANGE:
        return;
      default:
        ct('不支持的消息类型，请升级App');
    }
  }

  @override
  Widget build(BuildContext context) {
    return SafeScaffold(
      titleText: '系统通知',
      body: RefreshLoadView<SysNotify>(
        padding: const EdgeInsets.all(16),
        itemBuilder: (_, SysNotify s) => _buildItem(context, s),
        onRefresh: (_, __) async => await notifyData(
          pageNum: 1,
          conversationId: id,
        ),
        onLoad: (int page, __) async => await notifyData(
          pageNum: page,
          conversationId: id,
        ),
        splitWidget: const SizedBox(height: 20),
      ),
    );
  }

  /// 构建项
  Widget _buildItem(BuildContext context, SysNotify s) {
    return Column(
      children: <Widget>[
        Text(
          s.dateStr,
          style: TextStyle(
            color: black(context).withOpacity(0.3),
            fontSize: 12,
            fontWeight: FontWeight.bold,
          ),
        ),
        const SizedBox(height: 20),
        AppBtn(
          padding: EdgeInsets.zero,
          width: double.infinity,
          onTap: () => _onTap(context, s),
          radius: 10,
          child: _buildContent(context, s),
          color: white(context),
        ),
      ],
    );
  }

  Widget _buildContent(BuildContext context, SysNotify s) {
    switch (s.messageType) {
      case SysNotifyType.FRIEND_ADD:
        return FriendAdd(addObj: FriendAddObj.fromJson(s.content));
      case SysNotifyType.INTEGRAL_CHANGE:
        return PointChanged(obj: PointChangedObj.fromJson(s.content));
      default:
        return const DefItem();
    }
  }
}
